package com.mindainfo.groundwave.web.base.web;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by MD-01 on 2017/5/16.
 */
@Controller
public class BaseController {

    Logger logger = LoggerFactory.getLogger(BaseController.class);

    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String mainPage(){
        logger.info("跳转到主页面");
        return "/main";
    }

    @RequestMapping("/")
    public String home(){
        return "/main";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(){
        logger.info("跳转到登录页面");
        return "/login";
    }
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, HttpServletRequest request){
        logger.info("进行登录验证");
        UsernamePasswordToken token = new UsernamePasswordToken(username, password,request.getRemoteAddr());
        String msg = null;
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);

        } catch (AuthenticationException | UnknownSessionException e) {
            logger.debug("Error authenticating.", e);
            if (e instanceof UnknownAccountException) {
                msg = "未知账户！";
            } else if (e instanceof LockedAccountException) {
                msg = "账户已锁定！";
            } else if (e instanceof IncorrectCredentialsException) {
                msg = "密码不正确！";
            } else if (e instanceof ExcessiveAttemptsException) {
                msg = "用户名或密码错误超过5次,请5分钟后重试";
            } else if (e instanceof ConcurrentAccessException) {
                msg = "用户已登录！";
            } else if (e instanceof org.apache.shiro.authc.AccountException) {
                msg = "未知帐号错误或用户状态异常！";
            } else if (e instanceof AuthenticationException) {
                msg = "认证失败！";
            } else if (e instanceof UnknownSessionException){
                msg = "无效的session";
            }
            if (StringUtils.isEmpty(msg)) {
                msg = "用户名密码错误！";
            }
        }

        if(!StringUtils.isEmpty(msg)){
            System.out.println("msg:"+msg);
            request.getSession().setAttribute("errorMsg",msg);
            return "redirect:/login";
        }else{
            request.getSession().removeAttribute("errorMsg");
            return "redirect:/main";
        }
    }


    @RequestMapping("/welcome")
    public String welcome(){
        return "/welcome";
    }

    @RequestMapping("/401")
    public String error401(){
        return "/401";
    }

    @RequestMapping("/403")
    public String error403(){
        return "/403";
    }

    @RequestMapping("/404")
    public String error404(){
        return "/404";
    }


    @RequestMapping("/500")
    public String error500(){
        return "/500";
    }

    @RequestMapping("/test/error")
    public String testError(){
        throw new RuntimeException("hah");
    }

    @RequestMapping("/demoModal")
    public String demoModal(){
        return "/modalDemo";
    }
}
